UpdateCustomerCommandHandler.execute   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 19
c 0
b 0
f 0
rs 9.6
cc 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateCustomerCommand } from './UpdateCustomerCommand';
4
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository';
5
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException';
6
import { IsCustomerAlreadyExist } from 'src/Domain/Customer/Specification/IsCustomerAlreadyExist';
7
import { CustomerAlreadyExistException } from 'src/Domain/Customer/Exception/CustomerAlreadyExistException';
8
9
@CommandHandler(UpdateCustomerCommand)
10
export class UpdateCustomerCommandHandler {
11
  constructor(
12
    @Inject('ICustomerRepository')
13
    private readonly customerRepository: ICustomerRepository,
14
    private readonly isCustomerAlreadyExist: IsCustomerAlreadyExist
15
  ) {}
16
17
  public async execute(command: UpdateCustomerCommand): Promise<void> {
18
    const { id, name } = command;
19
20
    const customer = await this.customerRepository.findOneById(id);
21
    if (!customer) {
22
      throw new CustomerNotFoundException();
23
    }
24
25
    if (
26
      name !== customer.getName() &&
27
      true === (await this.isCustomerAlreadyExist.isSatisfiedBy(name))
28
    ) {
29
      throw new CustomerAlreadyExistException();
30
    }
31
32
    customer.updateName(name);
33
34
    await this.customerRepository.save(customer);
35
  }
36
}
37